package cn.iocoder.yudao.module.promotion.controller.admin.points;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.promotion.controller.admin.points.pointsproduct.*;
import cn.iocoder.yudao.module.promotion.convert.points.PointsProductConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.points.PointsProductDO;
import cn.iocoder.yudao.module.promotion.service.points.PointsProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 积分商品")
@RestController
@RequestMapping("/promotion/points-product")
@Validated
public class PointsProductController {

    @Resource
    private PointsProductService pointsProductService;

    @PostMapping("/create")
    @Operation(summary = "创建积分商品")
    @PreAuthorize("@ss.hasPermission('promotion:points-product:create')")
    public CommonResult<Long> createPointsProduct(@Valid @RequestBody PointsProductCreateReqVO createReqVO) {
        return success(pointsProductService.createPointsProduct(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新积分商品")
    @PreAuthorize("@ss.hasPermission('promotion:points-product:update')")
    public CommonResult<Boolean> updatePointsProduct(@Valid @RequestBody PointsProductUpdateReqVO updateReqVO) {
        pointsProductService.updatePointsProduct(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除积分商品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('promotion:points-product:delete')")
    public CommonResult<Boolean> deletePointsProduct(@RequestParam("id") Long id) {
        pointsProductService.deletePointsProduct(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得积分商品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('promotion:points-product:query')")
    public CommonResult<PointsProductRespVO> getPointsProduct(@RequestParam("id") Long id) {
        PointsProductDO pointsProduct = pointsProductService.getPointsProduct(id);
        return success(PointsProductConvert.INSTANCE.convert(pointsProduct));
    }

    @GetMapping("/list")
    @Operation(summary = "获得积分商品列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('promotion:points-product:query')")
    public CommonResult<List<PointsProductRespVO>> getPointsProductList(@RequestParam("ids") Collection<Long> ids) {
        List<PointsProductDO> list = pointsProductService.getPointsProductList(ids);
        return success(PointsProductConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得积分商品分页")
    @PreAuthorize("@ss.hasPermission('promotion:points-product:query')")
    public CommonResult<PageResult<PointsProductRespVO>> getPointsProductPage(@Valid PointsProductPageReqVO pageVO) {
        PageResult<PointsProductDO> pageResult = pointsProductService.getPointsProductPage(pageVO);
        return success(PointsProductConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出积分商品 Excel")
    @PreAuthorize("@ss.hasPermission('promotion:points-product:export')")
    //@OperateLog(type = EXPORT)
    public void exportPointsProductExcel(@Valid PointsProductExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PointsProductDO> list = pointsProductService.getPointsProductList(exportReqVO);
        // 导出 Excel
        List<PointsProductExcelVO> datas = PointsProductConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "积分商品.xls", "数据", PointsProductExcelVO.class, datas);
    }

}
